//
//  Track.m
//  SHPE Conference Companion
//
//  Created by Richard Kirk on 3/25/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "Track.h"
#import "Conference.h"
#import "Session.h"


@implementation Track
@dynamic id;
@dynamic name;
@dynamic desc;
@dynamic sessions;
@dynamic conferences;
@synthesize parent;

@synthesize xmlParser, xmlStringBuilder;


-(id)initWithParser:(NSXMLParser *)parser attributeDict:(NSDictionary *)attributes parent:(id)parentObject
{
    [super initWithEntity:[NSEntityDescription entityForName:@"Track" inManagedObjectContext:[self managedObjectContext] ] insertIntoManagedObjectContext:[self managedObjectContext]];
    
    [parser setDelegate:self];
    [self setParent:parentObject];
    
    self.id = [NSNumber numberWithInt:[[attributes objectForKey:@"id"] intValue]];
    self.name = [NSString stringWithString:[attributes objectForKey:@"name"]];

    
    return self;
}



-(id)initWithTrackId:(int)trackId
{
    
    
    [super initWithEntity:[NSEntityDescription entityForName:@"Track" inManagedObjectContext:[self managedObjectContext] ] insertIntoManagedObjectContext:[self managedObjectContext]];
    
  
    xmlParser = [NSXMLParser alloc];
    xmlParser.delegate = self;
    
    [xmlParser initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://openshpe.staging.shpe.org/conference/v1/track/%d", trackId]]];    
    
    if (xmlParser){
        [xmlParser parse];
    }
    
    return self;

}

-(NSString *)description
{
    
    
    return [NSString stringWithFormat:@"\nTrackid : %@\nName : %@\nDescription : %@\n", self.id, self.name,self.desc];
    
}



- (void)addSessionsObject:(Session *)value {    
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"sessions" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"sessions"] addObject:value];
    [self didChangeValueForKey:@"sessions" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [changedObjects release];
}

- (void)removeSessionsObject:(Session *)value {
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"sessions" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"sessions"] removeObject:value];
    [self didChangeValueForKey:@"sessions" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [changedObjects release];
}

- (void)addSessions:(NSSet *)value {    
    [self willChangeValueForKey:@"sessions" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"sessions"] unionSet:value];
    [self didChangeValueForKey:@"sessions" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
}

- (void)removeSessions:(NSSet *)value {
    [self willChangeValueForKey:@"sessions" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"sessions"] minusSet:value];
    [self didChangeValueForKey:@"sessions" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
}


- (void)addConfrencesObject:(Conference *)value {    
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"confrences" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"confrences"] addObject:value];
    [self didChangeValueForKey:@"confrences" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [changedObjects release];
}

- (void)removeConfrencesObject:(Conference *)value {
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"confrences" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"confrences"] removeObject:value];
    [self didChangeValueForKey:@"confrences" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [changedObjects release];
}

- (void)addConfrences:(NSSet *)value {    
    [self willChangeValueForKey:@"confrences" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"confrences"] unionSet:value];
    [self didChangeValueForKey:@"confrences" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
}

- (void)removeConfrences:(NSSet *)value {
    [self willChangeValueForKey:@"confrences" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"confrences"] minusSet:value];
    [self didChangeValueForKey:@"confrences" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
}



-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    
    if ( ( [elementName isEqualToString:@"conference-track"] )) 
    {
        self.id = [NSNumber numberWithInt:[[attributeDict objectForKey:@"id"] intValue]];
        self.name = [NSString stringWithString:[attributeDict objectForKey:@"name"]];
        return;
    }

    
    
    
    
    
}


-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    
    
    string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
 
    string = [string stringByReplacingOccurrencesOfString:@"&amp;" withString:@" & "];
    string = [string stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
    string = [string stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
  
    
    if (!xmlStringBuilder) 
    {
        xmlStringBuilder = [[NSMutableString alloc] initWithCapacity:100];
    }
          
    
    
    [xmlStringBuilder appendString:string];
    
}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    
    if ( [elementName isEqualToString:@"conference-track"] ) 
    {
            // NSLog(@"Printing Description :\n%@\n",self);
        [parser setDelegate:parent];
        xmlStringBuilder = nil;
        return;
    }
        
        
    else if( [elementName isEqualToString:@"description"] ) {
        self.desc = [NSString stringWithString:xmlStringBuilder]; ;
    }

    
    xmlStringBuilder = nil;
    
    
}


-(void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
    
    NSLog(@"XML ERROR :\n%@\n",[parseError description]);
    
    UIAlertView *parserAlert = [[UIAlertView alloc]init];
    
    
    [parserAlert setTitle:@"Parsing Error!"];
    
    [parserAlert setMessage:[NSString stringWithFormat:@"Error %i, Description: %@, Line: %i, Column: %i", 
                             [parseError code], 
                             [parseError localizedDescription], 
                             [parser lineNumber], 
                             [parser columnNumber]]];
    
    [parserAlert addButtonWithTitle:@"OK"];
    [parserAlert show];
    [parserAlert release];
    
    
    
}







@end
